package dry.example.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrameWithDialog {

    public static void main(String[] args) {
        JFrame frame = new JFrame("主窗口");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton showDialogButton = new JButton("显示对话框");
        showDialogButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog dialog = new JDialog(frame, "对话框", true); // 'true' 表示该对话框为模式对话框
                dialog.setSize(200, 150);
                JLabel label = new JLabel("这是一个模式对话框", SwingConstants.CENTER);
                dialog.add(label);
                dialog.setLocationRelativeTo(frame); // 让对话框居中显示在主窗口
                dialog.setVisible(true);
            }
        });

        frame.add(showDialogButton, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}
